<?php


namespace app\manager\controller;


use app\common\controller\Common;
use app\common\model\CardsCate;
use Redis;
use think\Db;
use think\Exception;
use think\facade\Log;

class Cards extends Common
{

    public function lists()
    {//会员卡列表
        if ($this->request->isAjax()) {
            $param = request()->param('key');
            $where = $param['key'];
            if (is_null(count($param['key']))) $where = [];
            $limit = $this->getPageSize();
            $cards = \app\common\model\Cards::where($where)->with('member')->order('status', 'desc')->paginate($limit);
            if (count($cards) == 0) return $this->errJson('暂无数据');
            return $this->listJson($cards->items(), $cards->total());
        } else {
            $reCC = CardsCate::select();
            if (count($reCC) > 0) {
                $this->assign('cardscate', $reCC);
            }
            return view();
        }
    }

    public function buildcards()
    {//批量生成会员卡
        $num = request()->param('num');
        $cc_id = request()->param('cc_id');
        if (is_null($num) || is_null($cc_id)) return $this->errJson('参数错误');

        $reCardsCate = CardsCate::where(['cc_id' => $cc_id, 'cc_status' => 2])->find();

        if ($reCardsCate) {
            $cate_title = $reCardsCate->cc_title;
            $price = $reCardsCate->cc_price;
            $term = $reCardsCate->cc_term;
        } else {
            return $this->errJson('数据异常');
        }
        $dnos = [];
        $x = 0;
        $reCards = Db::query("SELECT * FROM cards ORDER BY card_no DESC limit 1");
        if ($reCards[0]) {
            $card_no = $reCards[0]['card_no'];
            $x = intval($card_no) + 1;
        }
        $len = 8;
        for ($i = 0; $i < $num; $i++) {
            $numZero = $len - strlen($x);
            $dno['card_no'] = str_repeat('0', $numZero) . $x;
            $cardNos[$i]=str_repeat('0', $numZero) . $x;
            $dno['cate_title'] = $cate_title;
            $dno['price'] = $price;
            $dno['term'] = $term;
            $dno['time'] = time();
            $dnos[] = $dno;
            $x++;
        }
        Db::startTrans();
        try {
            $Cards = new \app\common\model\Cards();
            $ret = $Cards->saveAll($dnos);
            if ($ret) {
                shuffle($cardNos);
                $redis = new Redis();
                $redis->connect('127.0.0.1', 6379);
                $cardsLen = $redis->lLen('cardNos');
                $retRedis = $redis->lPush('cardNos', ...$cardNos);
                if (count($cardNos) + $cardsLen == $retRedis) {
                    Db::commit();
                    return $this->okJson('会员卡已生成');
                }else{
                    Db::rollback();
                    return $this->okJson('生成失败');
                }
            }
        }catch (\Exception $exception){
            Db::rollback();
        }
        return $this->errJson('生成失败');
    }

    public function listsCate()
    {//会员卡类型
        if ($this->request->isAjax()) {
            $limit = $this->getPageSize();
            $reCC = CardsCate::where('cc_status', 2)->paginate($limit);
            if (count($reCC) > 0) {
                return $this->listJson($reCC->items(), $reCC->total());
            }
            return $this->errJson('暂无数据');
        } else {
            return view();
        }
    }

    public function addCate()
    {//增加会员卡类型
        $title = input('title');
        $price = input('price');
        $term = input('term');
        if (is_null($title) || is_null($price) || is_null($term)) return $this->errJson('参数错误');
        $CardsCate = new CardsCate();
        $CardsCate->cc_title = $title;
        $CardsCate->cc_price = $price;
        $CardsCate->cc_term = $term;
        $ret = $CardsCate->save();
        if ($ret) {
            return $this->okJson('创建成功');
        } else {
            return $this->errJson('创建失败');
        }

    }

    public function setCardStatus()
    {//设置卡的状态
        $cardid = input('cardid');
        if (is_null($cardid)) return $this->errJson('参数错误');
        $Cards = \app\common\model\Cards::get($cardid);
        if ($Cards->status == 2) $Cards->status = 5;
        if ($Cards->status == 5) $Cards->status = 2;
        $ret = $Cards->save();
        if ($ret) return $this->okJson('ok');
        return $this->errJson('err');
    }

    public function setCardCate()
    {//设置会员卡类型的状态
        $cc_id = input('cc_id');
        if (is_null($cc_id)) return $this->errJson('参数错误');
        $CardsCate = CardsCate::get($cc_id);
        $CardsCate->cc_status == 2 ? $CardsCate->cc_status = -1 : $CardsCate->cc_status = 2;
        $ret = $CardsCate->save();
        if ($ret) return $this->okJson('ok');
        return $this->errJson('err');
    }
}